 <?php

Connection::getConnect();
 if (COUNT($_POST) && $this->get('list') != 1)
 {
 	if ($this->get('delete') == 3)
	{
		$id = $this->get('id');
		$sql = "DELETE FROM supplier_details WHERE sdid=$id";
		Connection::setQuery($sql);
		exit;
	}
 	if ($this->get('edit') != 0)
	{
		$name = $this->get('sname');
  		$rep = $this->get('srep');
  		$con = $this->get('scon');
  		$email = $this->get('semail');
  		$add = $this->get('sadd');
		$sdid = $this->get('edit');
       	$sql = "UPDATE supplier_details SET name='%s', representative='%s', contact='%s', email='%s', address='%s' WHERE sdid=%d;";
		Connection::setQueryf($sql, $name, $rep, $con, $email, $add, $sdid);
		echo "Successfully Updated!";
		
		$this->logger(INVENTORY, 'Supplier Updated - name="' . mysql_real_escape_string($name) . '".');
	} else {
		$name = $this->get('sname');
     	$sql = "SELECT sdid FROM supplier_details WHERE name='%s'";
        Connection::setQueryf($sql, $name);
    	$isuni =  Connection::fetch();
    	if ($isuni[0] == 0){
    		$name = $this->get('sname');
    		$rep = $this->get('srep');
    		$con = $this->get('scon');
    		$email = $this->get('semail');
    		$add = $this->get('sadd');
         	$sql = "INSERT INTO supplier_details(name, representative, contact, email, address) VALUES('$name','$rep','$con','$email','$add')";
            Connection::setQuery($sql);
			echo mysql_insert_id();
			$this->logger(INVENTORY, 'Supplier Added - name="' . mysql_real_escape_string($name) . '".');
    	}else{
    		echo "Already Exist!";
    	}
	}
	exit;
 }
 	$sql = "SELECT * FROM supplier_details";
    Connection::setQuery($sql);
	$supply_details = Connection::fetchAll(MYSQL_ASSOC);
	if ($this->get('list') != 1){
    	$sql = "SELECT * FROM item_unit_detail";
    	Connection::getConnect();
        Connection::setQuery($sql);
    	$measure = Connection::fetchAll(MYSQL_ASSOC);
	}

 ?>
 
